<template>
    <div class="top">
        <div class="choose">
                 <span>
                  描述：<input type="text" class="search_child" placeholder="如:描述" v-model="searchValue"></span>
            <span>
                  时间：<input type="date" class="search_child" v-model="privateSpacePage.dateTime"></span>
            <span  class="search_click" @click="search_">搜索</span>
        </div>
        <span class="add" @click="add">
             十 &nbsp;新增
        </span>
    </div>


    <div class="bottom">
        <table cellpadding="0" cellspacing="0">
            <thead>
            <tr style="height: 45px">
                <th>序号</th>
                <th>ID</th>
                <th>封面</th>
                <th style="min-width: 100px">发表日期</th>
                <th style="min-width: 500px">内容</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="(item,index) in fuzzyQueries" v-if="fuzzyQueries.length>0">
                <td>{{ total.current > 1 ? (index + 1) + 6 * (total.current - 1) : index + 1 }}</td>
                <td v-html="highlight_(item.id)"></td>
                <td>
                    <p v-if="!item.triviaImage">无</p>
                    <img v-else :src=" item.triviaImage" style="width:150px;height: 90px;margin-top: 5px" alt="" title="琐事">
                </td>
                <td>{{ item.time }}</td>
                <td class="overflow" v-html="highlight_(item.content)"></td>
                <td>
                    <span class="edit" @click="privateSpace_(item)">编辑</span>
                    <span class="edit del" @click="del(item,index)">删除</span>
                </td>
            </tr>
            <tr v-else-if="fuzzyQueries.length===0 && load_" style="height: 50vh;background: transparent">
                <LoadTheAnimation></LoadTheAnimation>
            </tr>
            <tr v-show="!load_">
                <td></td>
                <td></td>
                <td></td>
                <td><h2>未查询到任何数据</h2></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="bottom_">
        <span style="margin-right:10px">共{{ total.page }}页, 共{{ total.sum }}条数据 当前第{{ total.current }}页</span>
        <span class="jian" @click="left_">
               <img src="../../../public/static/jian_r.png" alt="">
          </span>
        <input type="text" class="limit" v-model="total.current" @focusout="limit_">
        <span class="jian" @click="right_">
               <img src="../../../public/static/jian_l.png" alt="">
          </span>
        <select @change ="page_($event)" class="page_index">
            <option  v-for="(item,index) in 3" >{{(index+1)*5+1}}/条</option>
        </select>
    </div>
    <PromptWindow v-show="box_confirm" @confirm="confirm" :info="info" :index="del_index"></PromptWindow>
    <PrivateSpaceEdit v-if="shutDown" :privateSpace="privateSpace" @shutDown="shutDown_"></PrivateSpaceEdit>
</template>

<script>
import api from "@/api";
import PrivateSpaceEdit from "@/components/privateSpace/PrivateSpaceEdit.vue";
import PromptWindow from "@/components/publicCommon/PromptWindow.vue";
import LoadTheAnimation from "@/components/publicCommon/LoadTheAnimation.vue";


export default {
    name: "PrivateSpace",
    components: {LoadTheAnimation, PromptWindow, PrivateSpaceEdit},
    data() {
        return {
            select: false,
            searchValue: "",
            fuzzyQueries: "",
            index: 1,
            condition: 1,
            privateSpacePage: {
                index: 1,
                condition: null,
                page:6,
                dateTime:null,
            },
            total: {
                sum: '',
                page: '',
                size: '',
                current: '',
            },
            privateSpace: {
                id: "",
                content: "",
                triviaImage: "",
            },
            shutDown: false,
            info: "",
            box_confirm: false,
            del_index: "",
            del_info: false,
            load_: true,
        }
    },
    watch: {
        'privateSpacePage.page'(newValue,oldValue){
            this.privateSpacePage.page = newValue;
            this.request_(this.privateSpacePage);
        },
        'privateSpacePage.dateTime'(newValue,oldValue){
            if(newValue == "") {
                this.privateSpacePage.dateTime = null
                this.request_(this.privateSpacePage);
            }
        },
        searchValue(newValue, oldValue) {
                if (this.searchValue === "") {
                    this.privateSpacePage.condition = null;
                    this.request_(this.privateSpacePage);
                }else{
                    this.privateSpacePage.condition = newValue;
                }
        },
        del_info(newValue, oldValue) {
            let _this = this;
            if (newValue) {
                api.delTrivia({delImg:_this.info.triviaImage,id:_this.info.id}).then((res) => {
                    if (res.data.code === 200) {
                        this.request_(this.privateSpacePage);
                        if (_this.privateSpacePage.index > this.total.page) {
                            _this.privateSpacePage.index = 1;
                        }
                    }
                })
                this.info = "";
                this.del_info = false;
            }
        },
        'total.current'(newVal, oldVal) {
            let reg = /^\+?[1-9]\d*$/;
            if (reg.test(newVal)) {
                this.privateSpacePage.index = newVal;
            }
            if (newVal > this.total.page) {
                this.privateSpacePage.index = 1;
            }
        }
    },
    mounted() {
        this.request_(this.privateSpacePage);
    },
    methods: {
        page_(event){
            this.privateSpacePage.page = parseInt(event.target.value);
        },
        confirm(index, bool) {
            this.box_confirm = false;
            if (bool) {
                this.del_info = true;
                this.fuzzyQueries.splice(index, 1);
            }
        },
        shutDown_() {
            this.shutDown = false;
            this.request_(this.privateSpacePage);
        },
        add() {
            this.privateSpace.id = ""
            this.privateSpace.content = ""
            this.privateSpace.triviaImage = ""
            this.shutDown = true;
        },
        privateSpace_(item) {
            if (!this.shutDown) {
                this.privateSpace = item;
            }
            this.shutDown = true;
        },

        limit_() {
            this.request_(this.privateSpacePage);
        },
        condition_(condition) {
            this.privateSpacePage.condition = condition;
            this.privateSpacePage.index = 1;
            this.request_(this.privateSpacePage);
            this.searchValue = "";
        },
        request_(articleInformationPage) {
            api.getTrivia(articleInformationPage).then((res) => {
                console.log(res)
                if (res.data.code === 200) {
                    this.fuzzyQueries = res.data.data.records;
                    this.total.sum = res.data.data.total;
                    this.total.page = res.data.data.pages;
                    this.total.current = res.data.data.current;
                    this.total.size = res.data.data.size;
                    if (this.fuzzyQueries.length === 0) {
                        setTimeout(() => {
                            this.load_ = false;
                        }, 1000)
                    } else {
                        this.load_ = true;
                    }
                } else {
                    setTimeout(() => {
                        this.load_ = false;
                    }, 200)
                }
            })
        },
        search_() {
            this.request_(this.privateSpacePage)
        },
        highlight_(text) {
            if(text === "" || text == null){
                return "无内容";
            }
            text = text + "";
            if (this.searchValue !== "")
                return text.replace(this.searchValue, '<span style="background: #f6f604;">' + this.searchValue + '</span>')
            else return text
        },
        left_() {
            this.privateSpacePage.index -= 1;
            if (this.privateSpacePage.index < 1) {
                this.privateSpacePage.index = 1;
                this.$store.commit("requestInfo", "当前已是第一页!");
                return;
            }
            this.request_(this.privateSpacePage);
        },
        right_() {
            this.privateSpacePage.index += 1;
            if (this.privateSpacePage.index > this.total.page) {
                this.privateSpacePage.index = this.total.page;
                this.$store.commit("requestInfo", "已是最后一页!");
                return;
            }
            this.request_(this.privateSpacePage);
        },
        del(info, index) {
            this.info = info;
            this.del_index = index;
            this.box_confirm = true;
        }
    }
}
</script>

<style scoped>

.add {
    padding: 5px 6px;
    color: white;
    background: #3586f1;
    border-radius: 3px;
    font-size: 14px;
    cursor: pointer;
}

.add:hover {
    opacity: .9;
}
.search_child{
    height: 30px;
    padding:0 10px;
    outline:none;
}
.choose{
    margin-left: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.choose span{
    margin-right: 10px;
}
.search_click{
    display: inline-block;
    background: #057cf8;
    padding: 4px 7px;
    color: white;
    cursor: pointer;
}
.search_click:hover{
    opacity: .8;
}
.page_index{
    height: 26px;
    outline: none;
    padding: 0 7px;
    color: #4297ec;
    border: 1px solid #282828;
}
option{
    padding: 10px;
}
</style>